package cn.zyy.hotel.service.impl;

import cn.zyy.hotel.dao.RoleMapper;
import cn.zyy.hotel.entity.Role;
import cn.zyy.hotel.service.RoleService;
import cn.zyy.hotel.vo.RoleVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    //查询角色列表
    public List<Role> selectRoleList(RoleVO roleVo) {
        return roleMapper.selectRoleList(roleVo);
    }

    //添加角色
    public int addRole(Role role) {
        return roleMapper.addRole(role);
    }

    //修改角色
    public int updateRole(Role role) {
        return roleMapper.updateRole(role);
    }

    //删除角色
    public int deleteById(Integer id) {
        return roleMapper.deleteById(id);
    }

    //保存角色菜单关系
    public int saveRoleMenu(String ids, Integer roleId) {
        try{
            //删除原有的菜单关系  根据角色id删除
            roleMapper.deleteRoleMenu(roleId);
            //将ids拆分成数组
            String[] idsStr = ids.split(",");
            //循环调用
            for (int i = 0; i < idsStr.length; i++) {
                //调用保存菜单角色关系的方法
                roleMapper.addRoleMenu(roleId,idsStr[i]);

            }
            //成功
            return 1;
        }catch (Exception e){
            e.printStackTrace();
        }
        //失败
        return 0;
    }

    //查询所有角色列表
    public List<Map<String, Object>> selectRoleListByMap() {
        return roleMapper.selectRoleListByMap();
    }

    //根据员工ID查询该员工拥有的角色列表
    public List<Integer> selectEmployeeRoleByEmployeeId(Integer employeeId) {
        return roleMapper.selectEmployeeRoleByEmployeeId(employeeId);
    }


}
